package com.zxy.car.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zxy.car.dao.LuDao;
import com.zxy.car.entity.LuEntity;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ElmRedis implements ApplicationRunner {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private LuDao luDao;

    @Autowired
    RestHighLevelClient client;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("项目启动列表存redis");
        List<LuEntity> list = luDao.selectList(null);
        if (list!=null){
            for (LuEntity luEntity : list) {
                IndexRequest request = new IndexRequest("sh");
                request.id(luEntity.getPlan()+"");
                String jsonString = JSONObject.toJSONString(luEntity);
                request.source(jsonString, XContentType.JSON);
                IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
            }
        }
        for (LuEntity luEntity : list) {
            redisTemplate.opsForValue().set(luEntity.getPlan()+"sh", JSON.toJSONString(luEntity.getPlan()));
        }

    }
}
